package com.lanhai.longheng.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson; 
import com.lanhai.longheng.beans.DeviceMountBean;
import com.lanhai.longheng.interfaces.IDeviceMount;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 设备上下料操作
 * @author zhengwei
 *
 */
@Service 
public class DeviceMountImpl implements IDeviceMount {
	@Autowired
	JedisPool jedisPool;
	
	static Map<Long,DeviceMountBean> mountMap=  Collections.synchronizedMap(new HashMap<Long,DeviceMountBean>()); 
	static Object locker =new Object();
	
	
	/**
	 * 获取redis中的数据
	 */
	@Override
	public   DeviceMountBean getBean(long deviceid) { 
		synchronized (locker) {
		 
		DeviceMountBean bean=mountMap.get(deviceid);
		if(bean==null) {
			String mainkey="deviceMount";
			String key=String.format("%d", deviceid);
			Gson gson=new Gson();
		 
			Jedis jd=jedisPool.getResource(); 
			String json=jd.hget(mainkey, key);
			jd.close();
			if(json!=null&&json.isEmpty()==false) {
				try {
					bean=gson.fromJson(json, DeviceMountBean.class);
				}catch(Exception e) { 
				}
			}
			if(bean!=null) {
				mountMap.put(deviceid, bean);
			}else {
				bean=new DeviceMountBean();
				bean.setDeviceid(deviceid); 
				bean.setAllowIn(false);
				bean.setAllowOut(false);
				bean.setInNum(0);
				bean.setOutNum(0);
				mountMap.put(deviceid, bean);
				
				//原本就没有，就没必要写进redis里
			}
		} 
		return bean;
		}
	}

	/**
	 *保存数据
	 */
	@Override
	public synchronized void saveCache(long deviceid, DeviceMountBean bean) {
		synchronized (locker) {
		 
		mountMap.put(deviceid, bean);
		
		String mainkey="deviceMount";
		String key=String.format("%d", deviceid); 
		Gson gson=new Gson();
		String json=gson.toJson(bean);
		Jedis jd=jedisPool.getResource();
		jd.hset(mainkey,key, json);
		jd.close();
		}
	}

	/**
	 *从redis中移除
	 */
	@Override
	public   void clear(long deviceid) {
		synchronized (locker) {

			DeviceMountBean bean = mountMap.get(deviceid);
			if (bean == null) {
				bean = new DeviceMountBean();
				bean.setDeviceid(deviceid);
			}
			bean.setDeviceid(deviceid);
			bean.setAllowIn(false);
			bean.setAllowOut(false);
			bean.setInNum(0);
			bean.setOutNum(0);
			mountMap.put(deviceid, bean);

			String mainkey = "deviceMount";
			String key = String.format("%d", deviceid);
			Gson gson = new Gson();
			String json = gson.toJson(bean);
			Jedis jd = jedisPool.getResource();
			jd.hset(mainkey, key, json);
			jd.close();
		}
	}

	/**
	 * 手动将机台数量填满，达到传输要求
	 */
	@Override
	public void fullDeviceMount(long deviceid) {
		DeviceMountBean bean=mountMap.get(deviceid);
		if(bean==null) {
			bean=new DeviceMountBean(); 
			bean.setDeviceid(deviceid);
		} 
		bean.setDeviceid(deviceid); 
		bean.setAllowIn(true);
		bean.setAllowOut(true);
		bean.setInNum(10);
		bean.setOutNum(10); 
		mountMap.put(deviceid, bean);
		 
		String mainkey="deviceMount";
		String key=String.format("%d", deviceid);
		Gson gson=new Gson();  
		String json =gson.toJson(bean); 
		Jedis jd=jedisPool.getResource(); 
		jd.hset(mainkey, key, json);
		jd.close();
	}
 
}
